package ru.st.selenium.util;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;

import java.util.Calendar;
import java.util.Random;
import java.util.TimeZone;

/**
 * Created with IntelliJ IDEA.
 * User: gor
 * Date: 20.10.13
 * Time: 19:19
 * To change this template use File | Settings | File Templates.
 */
public class MyUtil {

    private static boolean acceptNextAlert = true;

    public static boolean isElementPresent(WebDriver driver, By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }



    public static String closeAlertAndGetItsText(WebDriver driver) {
        String result = "";
        try {
            Alert alert = driver.switchTo().alert();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alert.getText();
        } catch (Exception e) {

        } finally {
            acceptNextAlert = true;
        }
        return result;
    }

    public static int getRnd(){
        Random rnd = new Random();
        return rnd.nextInt(65535);
    }

    public static String getFullDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+4"));

        String result = "";
        String year = Integer.toString(calendar.get(Calendar.YEAR));
        String month = Integer.toString(calendar.get(Calendar.MONTH) + 1);
        month = (month.length() == 1) ? "0" + month : month;
        String day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
        day = (day.length() == 1) ? "0" + day : day;

        String mi = Integer.toString(calendar.get(Calendar.MINUTE));
        mi = (mi.length() == 1) ? "0" + mi : mi;
        String ss = Integer.toString(calendar.get(Calendar.SECOND));
        ss = (ss.length() == 1) ? "0" + ss : ss;
        String offset = Integer.toString(calendar.get(Calendar.ZONE_OFFSET) / (1000 * 60 * 60));
        int am_pm = calendar.get(Calendar.AM_PM);
        int hour = calendar.get(Calendar.HOUR);
        if (am_pm == 1) hour += 12;
        String hh = Integer.toString(hour);
        hh = (hh.length() == 1) ? "0" + hh : hh;

//        result = hh + ":" + mi + ":" + ss + " " + day + "." + month + "/" + year + " " + am_pm + " " + offset;
        result = hh + ":" + mi + ":" + ss + " " + day + "." + month + "." + year;
        return result;
    }


    public static StringBuffer softAssert(String observed, String expected){
        StringBuffer result = new StringBuffer();

        if (observed.compareTo(expected) == 0){
            System.out.println("!OK - Observed = " + observed + "; expected = " + expected);
        }
        else {
            System.out.println("!ERROR - Observed = " + observed + "; expected = " + expected);
            result.append("! см лог");
        }

        return result;
    }
}
